<?php
/*    This file is part of Nephite Exchange.
 *
 *    Nephite Exchange is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    Nephite Exchange is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with Nephite Exchange.  If not, see <http://www.gnu.org/licenses/>.
 */

class NephiteExchangeDataController extends Zend_Controller_Action
{
	public function preDispatch()
	{
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
	}

	public function init()
	{
		/* Initialize action controller here */
	}

	public function indexAction()
	{
		$neda = new Default_Model_NephiteExchangeData();
		echo $this->processRequest ($neda);
	}

	protected function processRequest (Default_Model_NephiteExchangeData $neda) {
		$request = $this->getRequest();

		$type = $request->getParam('type');
		if ($type == "last-updated") {
			return $neda->getLastUpdated();
		} else if ($type == "barley-price") {
			return $neda->getLatestBarleyPrice();
		} else if ($type == "comment") {
			$email = $request->getParam('email');
			$comment = $request->getParam('comment');
			
			return $this->addComment($neda, $email, $comment);
		} else if ($type == "currencies") {
			return $this->generateCurrenciesJSON($neda);
		} else if ($type == "convert") {
			$ISOName = $request->getParam('iso-name');
			$amount = $request->getParam('amount');

			if ($ISOName == "") {
				/* TODO Error on ISOName. */
			} else if ($amount == "") {
				/* TODO Error on amount. */
			}

			return $this->generateConvertJSON ($neda, $ISOName, $amount);
		} else {
			$search = $request->getParam('search');
			if ($search == "") {
				$search = null;
			}

			return $this->generateExchangeJSON ($neda, $search);
		}
	}

	protected function addComment (Default_Model_NephiteExchangeData $neda, $email, $comment) {
		$neda->addComment ($email, $comment);
	}
	
	protected function generateCurrenciesJSON (Default_Model_NephiteExchangeData $neda) {
		$json = "[";

		$currencies = $neda->getCurrencies();

		foreach ($currencies as $currency) {
			$json .= "{";

			$json .= "\"currency\": \"$currency\"";

			$json .= "},";
		}

		if (strlen ($json) > 1) {
			$json = substr ($json, 0, -1);
		}
		$json .= "]";

		return $json;
	}

	protected function generateExchangeJSON (Default_Model_NephiteExchangeData $neda, $search) {
		$json = "[";

		$exchanges = $neda->getExchangeRates($search);

		foreach ($exchanges as $exchange) {
			$json .= "{";

			$json .= "\"country\": \"" . $exchange->getCountry() . "\",";
			$json .= "\"currency\": \"" . $exchange->getCurrencyName() . "\",";
			$json .= "\"isoname\": \"" . $exchange->getISOName() . "\",";
			$json .= "\"exchange\": " . $exchange->getNephiteExchange($neda->getLatestBarleyPrice());

			$json .= "},";
		}

		if (strlen ($json) > 1) {
			$json = substr ($json, 0, -1);
		}
		$json .= "]";

		return $json;
	}

	protected function generateConvertJSON (Default_Model_NephiteExchangeData $neda, $ISOName, $amount) {

		$conversion = $neda->convertPrice($ISOName, $amount);

		$json = "[";

		if ($conversion !== -1) {
			$json .= "{";

			foreach ($conversion as $name => $value) {
				$json .= "\"$name\": $value,";
			}

			if (strlen ($json) > 1) {
				$json = substr ($json, 0, -1);
			}
			$json .= "}";
		}

		$json .= "]";
		return $json;

	}


}

